﻿using FamilyTree.BLL;
using FamilyTree.DLL;
using FamilyTree.Entity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Xml.Linq;

namespace FamilyTree.WebSite.Controllers
{
    public class FamilyInfoController : Controller
    {
        // GET: FamilyInfo
        public ActionResult Index(string edit)
        { 
            ViewBag.IsEdit = edit ;
            //ViewBag.SalerList = _customerApp.GetUserList(salersID);
            List<FamilyInfoEntity> cusList = BFamilyInfo.BLL.GetModels();
            return View(cusList); 
        }

        public ActionResult Index2(int fid,string edit)
        { 
            ViewBag.IsEdit = edit != "9" ? false:true;
            ViewBag.FID = fid;
            return View();
        }

        public ActionResult FamilyTree()
        {  
            return View( );
        }

        // GET: FamilyInfo/Details/5
        public ActionResult EFamily(int fid, string type)
        {
            FamilyInfoEntity entity = BFamilyInfo.BLL.GetModel(fid);
            ViewBag.JsonData = JsonConvert.SerializeObject(entity);
            ViewBag.Type = type;
            ViewBag.Member = entity;
            return View(entity);
        }

        public MemberEntity member;
        // GET: FamilyInfo/Create
        public ActionResult Item(int mid,string type,int fid)
        {
            ViewBag.Type = type;
            ViewBag.FId = fid;
            member = BMember.BLL.GetModel(mid);
            ViewBag.JsonData =  JsonConvert.SerializeObject(member);
            ViewBag.Member = member;
            return View(member);
        }


        [HttpPost]
        public ActionResult EditFamily(FamilyInfoEntity jsonData)
        {
            bool i = BFamilyInfo.BLL.Update(jsonData);

            var result = i ? 1 : 0;

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        // POST: FamilyInfo/Edit/5
        [HttpPost]
        public ActionResult Edit(MemberEntity jsonData)
        {
            bool i = BMember.BLL.Update(jsonData);

            var result = i ? 1 : 0;

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Save(MemberEntity jsonData)
        {
            int i = BMember.BLL.Insert(jsonData);
            var result = i > 0 ? 1 : 0;

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        // GET: FamilyInfo/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: FamilyInfo/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult GetJsonData(int fid)
        {
            List<Entity.MemberEntity> Menus = BMember.BLL.GetMemberInfos(fid);

            return Json(Menus, JsonRequestBehavior.AllowGet);
        }

    }
}
